package com.gbq.qimo.anno;

import com.gbq.qimo.mapper.AdminMapper;
import com.gbq.qimo.pojo.Admin;
import com.gbq.qimo.utils.JDBCUtils;
import org.omg.CORBA.portable.InvokeHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @ClassName MapperProxy
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/5/29 11:04
 */
public class MapperProxy implements InvocationHandler {
//    private final String url = "jdbc:mysql://localhost:3306/student_manager?Unicode=true&characterEncoding=UTF-8&autoReconnect=true&connectTimeout=60000&socketTimeout=60000&useInformationSchema=true";
//    private final String username = "root";
//    private final String pwd = "123456789g";
    private Class<?> mapperClass;

    private TableMetadata tableMetadata;

    public MapperProxy(Class<?> mapperClass) {
        this.mapperClass = mapperClass;
        this.tableMetadata = new TableMetadata(mapperClass);
    }

    public static <T> T getMapper(Class<T> mapperClass) {
        return (T) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), new Class[]{mapperClass}, new MapperProxy(mapperClass));
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        try (Connection connection = JDBCUtils.getConnection()) {
            //这里demo简化直接创建数据库连接，然后实现selectById方法
            if ("selectById".equals(method.getName())) {
                String selectByIdSql = tableMetadata.getSelectByIdSql();
                try (PreparedStatement preparedStatement = connection.prepareStatement(selectByIdSql)) {
                    preparedStatement.setObject(1, args[0]);
                    try (ResultSet resultSet = preparedStatement.executeQuery()) {
                        //反射创建实体类
                        Object entity = tableMetadata.getEntityClass().newInstance();
                        if (resultSet.next()) {
                            for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
                                //反射设置属性
                                columnMetadata.getField().setAccessible(true);
                                columnMetadata.getField().set(entity, resultSet.getObject(columnMetadata.getColumnIndex()));
                            }
                            return entity;
                        }
                    }

                }


            }
            else if ("insert".equals(method.getName())){
                 String insertSql = tableMetadata.getInsertSql();
                try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                     int i = preparedStatement.executeUpdate();

                }
            }
        }
        return null;
    }

//    public static void main(String[] args) {
//       AdminMapper adminMapper = MapperProxy.getMapper(AdminMapper.class);
//        Admin user = adminMapper.selectById(1);
//        System.out.println(user);
//    }
}
